package org.bozo.im.service.protocols.xmpp.session;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import org.bozo.im.service.Constants;
import org.bozo.im.service.protocols.xmpp.XMPPSession;
import org.bozo.im.service.protocols.xmpp.XMPPSessionConfiguration;
import org.bozo.im.service.protocols.xmpp.stanza.Stanza;
import org.bozo.im.service.protocols.xmpp.stanza.generator.StanzaGenerator;
import org.bozo.im.service.protocols.xmpp.util.namespace.Namespace;

import android.util.Log;

public class XMPPWriter {

	private PrintWriter mWriter;
	private XMPPSession mSession;

	public XMPPWriter(XMPPSession pSession, Socket pSocket) throws IOException {
		mWriter = new PrintWriter(pSocket.getOutputStream(), false);
		mSession = pSession;
	}

	public void writeHeader() {

		StringBuilder lBuilder = new StringBuilder();
		lBuilder.append("<?xml version='1.0'?>");
		write(lBuilder);
	}
	
	public void startStream() {

		mSession.getTaskProcessor().cancel();
		
		if (mSession.getConfiguration() != null) {
			XMPPSessionConfiguration lConfiguration = mSession.getConfiguration();

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ---------- Starting new session");
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ----------      To: " + lConfiguration.getTo());
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ----------      Server: " + lConfiguration.getServer());
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ----------      Lang: " + lConfiguration.getLang());
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ----------      Login: " + lConfiguration.getLoginJID());
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ----------      Is initial: " + (lConfiguration.getInitialStream() == null));
			// ---------------------------------------------------------------------------------------

			StanzaGenerator lNewStream = new StanzaGenerator(Stanza.STREAM_STREAM);
			lNewStream.setAttribute(Namespace.JABBER_CLIENT, "to", lConfiguration.getTo());
			lNewStream.setAttribute(Namespace.XML, "lang", lConfiguration.getLang());
			lNewStream.setAttribute(Namespace.JABBER_CLIENT, "version", "1.0");
			lNewStream.setDefaultNamespace(Namespace.JABBER_CLIENT);
			
			lConfiguration.setInitialStream(lNewStream);

			StringBuilder lBuilder = new StringBuilder();

			lNewStream.toXmlOpeningTag(lBuilder);
			write(lBuilder);

			// ---------------------------------------------------------------------------------------
			Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.startStream ---------- Opening XML stream: " + lBuilder);
			// ---------------------------------------------------------------------------------------
		}
	}

	public void closeStream() {

		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.closeStream ---------- Closing the session");
		// ---------------------------------------------------------------------------------------

		XMPPSessionConfiguration lConfiguration = mSession.getConfiguration();
		StringBuilder lBuilder = new StringBuilder();

		lConfiguration.getInitialStream().toXmlClosingTag(lBuilder);
		write(lBuilder);
		
		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.closeStream ---------- Closing XML stream: " + lBuilder);
		// ---------------------------------------------------------------------------------------
	}

	public void updateSocket(Socket pSocket) throws IOException {
		mWriter = new PrintWriter(pSocket.getOutputStream(), false);
	}
	
	void write(StringBuilder pBuilder) {
		// ---------------------------------------------------------------------------------------
		Log.d(Constants.ACTIVITY_TAG, "org.bozo.im.xmpp.session.XMPPWriter.write ---------- Writing content: " + pBuilder);
		// ---------------------------------------------------------------------------------------
		mWriter.append(pBuilder);
		mWriter.flush();
	}
}
